package com.qf.cachecenter.config;

import com.fasterxml.jackson.databind.ser.std.StringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Redis配置类
 * @Created by 马英超 on 2022/10/19 13:56
 * @author： 马英超
 * @version：v1.0
 * @description: .__
 * ___________________|__| ____    ____
 * /  ___/\____ \_  __ \  |/    \  / ___\
 * \___ \ |  |_> >  | \/  |   |  \/ /_/  >
 * /____  >|   __/|__|  |__|___|  /\___  /
 * \/ |__|                 \//_____/
 */
@Configuration
public class RedisTemplateConfig {

    @Bean
    public RedisTemplate redisTemplate(LettuceConnectionFactory factory){
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();//创建RedisTemplate对象
        redisTemplate.setConnectionFactory(factory);//通过连接工厂连接Redis

        //序列化key设置
        StringRedisSerializer redisSerializeredis = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializeredis);
        redisTemplate.setHashKeySerializer(redisSerializeredis);

        //序列化value设置
        Jackson2JsonRedisSerializer<String> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>(String.class);
        redisTemplate.setValueSerializer(jsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jsonRedisSerializer);
        return redisTemplate;
    }
}
